package com.yuneec.fly.presenters;

import com.yuneec.droneservice.model.Aircraft;
import com.yuneec.fly.events.EventBusFactory;
import com.yuneec.fly.events.uiEvent.BindEvent;
import com.yuneec.fly.events.uiEvent.OnResetEvent;
import com.yuneec.fly.events.uiEvent.ReflushEvent;
import com.yuneec.fly.events.uiEvent.ResetEvent;
import com.yuneec.fly.events.uiEvent.ScanEvent;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zd.zhang on 2016/7/15.
 */
public class BindModelPresenter extends AbstractBindPresenter {
    private List<Aircraft> datas = new ArrayList();
    private DroneStatePresenter manager = new DroneStatePresenter();

    @Override
    public void start(String bind) {
        super.start(bind);
        EventBusFactory.getUiBus().register(this);
    }

    @Override
    public void stop() {
        EventBusFactory.getUiBus().unregister(this);
        super.stop();
    }

    @Override
    public List getDatas() {
        return datas;
    }

    @Override
    public boolean removeIndex(int index) {
        if (index >= 0 && datas.size() > index) {
            datas.remove(index);
            return true;
        }
        return false;
    }

    @Override
    public String getCurrentBind() {
        Aircraft currentBindInfo = manager.getCurrentBindInfo();
        if (currentBindInfo != null) {
            return currentBindInfo.getName();
        } else {
            return null;
        }
    }

    @Override
    public String getName(int position) {
        return datas.get(position).getName();
    }

    @Override
    public int getSize() {
        return datas.size();
    }

    @Subscribe(threadMode = ThreadMode.BACKGROUND)
    public void scan(ScanEvent event) {
        List<Aircraft> res = manager.droneScan();
        Aircraft currentBindInfo = manager.getCurrentBindInfo();
        datas.clear();
        if (res != null) {
            for (Aircraft re : res) {
                datas.add(re);
            }
        }
        mChecked = -1;
        EventBusFactory.getUiBus().post(new ReflushEvent());
        resetState();
    }

    @Subscribe(threadMode = ThreadMode.BACKGROUND)
    public void bind(BindEvent event) {
        if (event.state == BindEvent.STATE_BIND && mChecked >= 0 && datas.size() > mChecked) {
            Aircraft bindInfo = datas.get(mChecked);
            boolean res = manager.droneBind(bindInfo.getId());
            BindEvent resEvent = new BindEvent();
            resEvent.state = BindEvent.STATE_BINDED;
            resEvent.tag = res ? bindInfo.getName() : null;
            EventBusFactory.getUiBus().post(resEvent);
        }
    }

    @Subscribe(threadMode = ThreadMode.BACKGROUND)
    public void onReset(ResetEvent event) {
        manager.droneUnBind();
        EventBusFactory.getUiBus().post(new OnResetEvent(true));
        EventBusFactory.getUiBus().post(new ScanEvent());
        resetState();
        com.yuneec.fly.utils.LogUtils.e("on reset");
    }

}
